package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.manage.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.common.enums.FileTypeEnum;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.IamFileMapper;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.manage.MaterialsStorageMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.File;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.manage.MaterialsStorage;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.accessory.IamOrganizationService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.manage.MaterialsInventoryService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.manage.MaterialsStorageService;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Organization;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by yangsy on 2016/12/8.
 */
public class MaterialsStorageServiceImpl implements MaterialsStorageService {
    @Autowired
    private MaterialsStorageMapper materialsStorageMapper;
    @Autowired
    private IamFileMapper iamFileMapper;
    @Autowired
    private MaterialsInventoryService materialsInventoryService;
    @Autowired
    private IamOrganizationService iamOrganizationService;

    @Override
    public int deleteByPrimaryKey(String materialsStorageId) {
        return materialsStorageMapper.deleteByPrimaryKey(materialsStorageId);
    }

    @Override
    public int insert(MaterialsStorage record) {
        return materialsStorageMapper.insert(record);
    }

    @Override
    public int insertSelective(MaterialsStorage record) {

        return materialsStorageMapper.insertSelective(record);
    }

    @Override
    public MaterialsStorage selectByPrimaryKey(String materialsStorageId) {
        return materialsStorageMapper.selectByPrimaryKey(materialsStorageId);
    }

    @Override
    public int updateByPrimaryKeySelective(MaterialsStorage record) {
        return materialsStorageMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(MaterialsStorage record) {
        return materialsStorageMapper.updateByPrimaryKey(record);
    }

    @Override
    public JSONObject list(MaterialsStorage materialsStorage) {
        JSONObject jsonObject = new JSONObject();
        materialsStorage.setLimit(materialsStorage.getLimit() == null ? 10 : materialsStorage.getLimit());
        materialsStorage.setOffset(materialsStorage.getOffset() == null ? 0 : materialsStorage.getOffset());
        jsonObject.put("total", materialsStorageMapper.queryCount(materialsStorage));
        jsonObject.put("rows", JSON.toJSON(materialsStorageMapper.queryList(materialsStorage)));
        return jsonObject;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insert(MaterialsStorage record, String uploadFiles, String deletedFiles) {
        //  插入数据成功之后，返回的条数
        int resultCount = 0;
        //  根据申请人  查询申请人所属组织
        Organization organization =  null;//iamOrganizationService.organization(record.getApplyUserId());
        //  申请单位
        Employee employee = iamOrganizationService.employee(record.getApplyUserId());
        String applyUnit = "";
        if(StringUtils.isNotEmpty(employee.getDepId())){
            applyUnit = employee.getDepId();
        }else{
            applyUnit = employee.getOrgId();
        }
        record.setApplyUnit(applyUnit);
        //  根据申请人 查询申请人所属作业区
        organization = iamOrganizationService.getWorkZone(record.getApplyUserId());
        //  对应库存
        record.setInventoryId(organization.getOrgId());
        //保存业务数据
        resultCount  +=  materialsStorageMapper.insertSelective(record);
        //更新的附件不为空的情况
        if(uploadFiles!=null && !uploadFiles.equals("")){
            //强制转换成集合
            List<File> listFiles =  JSON.parseArray(uploadFiles,File.class);
            if(listFiles!=null && listFiles.size()>0){
                //重新设置附件属性
                String pId = record.getMaterialsStorageId();
                int len = listFiles.size();
                for(int i=0;i<len;i++){
                    File _file = listFiles.get(i);
                    //设置主键
                    _file.setFileId(UniqueUtil.uuid());
                    //设置业务数据id
                    _file.setBusinessId(pId);
                    _file.setFileType(FileTypeEnum.getCode(_file.getExt()));
                }
                resultCount += iamFileMapper.insert(listFiles);
            }
        }
        //插入数据成功之后还要写入库存
        resultCount += materialsInventoryService.updateByMaterialsStorageInfo(record);
        return resultCount;
    }
}
